<template>

  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <el-input v-model="getListData.name" placeholder="请输入角色名" style="width: 200px;" class="filter-item" clearable />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getRoleList">搜索</el-button>
      <el-button class="filter-item" style="margin-left: 14px;" type="success" @click="handleEdit('','')">新增角色
      </el-button>

    </div>

    <!-- 表格 -->
    <el-table :key="tableKey" :data="tableData" v-loading="listLoading" highlight-current-row border>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="name" label="角色名" sortable></el-table-column>
      <el-table-column prop="created" label="创建时间"></el-table-column>
      <el-table-column label="操作" fixed="right">
        <template slot-scope="{row}">
          <el-button size="mini" type="primary" @click="handleEdit(row.id,row.name)">编辑</el-button>
          <!-- <el-button size="mini" type="danger" @click="handleDelete(row.id)">删除</el-button> -->
          <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' title='确认删除当前数据' @confirm="handleDelete(row.id)" style="margin-left:10px;">
            <el-button size="mini" type="danger" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="totalPage>0" :total="totalPage" :page.sync="getListData.pageNo"
      :limit.sync="getListData.pageSize" @pagination="getRoleList" />

    <!-- 编辑弹窗 -->
    <el-dialog title="编辑角色" :visible.sync="dialogVisible" width="30%" :close-on-click-modal='false'>
      <el-form :model="form" ref="roleNameForm">
        <el-input v-model="form.id" class="hide"></el-input>
        <el-form-item label="角色名" :rules="[{ required: true, message: '角色名不能为空'}]" prop="name">
          <el-input placeholder="请输入角色名" clearable v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doEditSave()">确 定</el-button>
      </span>
    </el-dialog>


  </div>

</template>


<script>
  import {
    Message
  } from 'element-ui'
  import Pagination from '@/app/components/Pagination' // secondary package based on el-pagination
  export default {
    components: {
      Pagination
    },
    name: 'AdminIndex',
    data() {
      return {
        // 表格
        tableKey: 0,
        tableData: [],
        totalPage: 1,
        listLoading:true,
        // 列表查询
        getListData: {
          pageNo: 1,
          pageSize: 10,
          name: ''
        },
        // 编辑弹窗
        dialogVisible: false,
        form: {
          id: '',
          name: ''
        },

      }
    },
    created() {
      this.getRoleList()
    },
    methods: {
      // 获取角色列表
      getRoleList() {
        this.listLoading = true
        this.$http.post('/admin/AuthController/getRoleList', this.getListData)
          .then((res) => {
            this.tableData = res.data.ret
            this.totalPage = res.data.count
            this.listLoading = false
          })
      },
      handleEdit(id, name) {
        this.dialogVisible = true
        this.form.name = name
        this.form.id = id
        this.$nextTick(() => {
          this.$refs['roleNameForm'].clearValidate()
        })
      },
      // 编辑角色名保存
      doEditSave() {
        this.$refs['roleNameForm'].validate((valid) => {
          if (valid) {
            this.$http.post('/admin/AuthController/saveRole', this.form)
              .then((res) => {
                console.log(res)
                if (res.code == '00000') {
                  Message({
                    message: '保存成功',
                    type: 'success',
                    duration: 5 * 1000
                  })
                  this.getRoleList();
                }
              })
            this.dialogVisible = false
          } else {
            return false;
          }
        })
      },

      // 删除角色
      handleDelete(id) {
        this.$http.post('/admin/AuthController/delRole',{id})
          .then((res) => {
            if (res.code == '00000') {
              Message({
                message: '操作成功',
                type: 'success',
                duration: 5 * 1000
              })
              this.getRoleList();
            }
          }).catch((error) => {
            console.log('handleDelete error', error)
          })
      },

    }
  };

</script>
